package controllers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"testGin/model/mysql"
	"testGin/model/tool"
	"time"
)

type UserController struct{}



func (ctr UserController) Index(c *gin.Context) {
	c.HTML(200, "system/user/index.html", gin.H{})
}
func (ctr UserController) GetList(c *gin.Context) {

	page := c.DefaultQuery("page", "1")
	limit := c.DefaultQuery("limit", "15")
	where := []map[string]interface{}{}
	pageInt, err := strconv.Atoi(page)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	limitInt, err := strconv.Atoi(limit)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	ctrinId := c.Query("ctrinId")
	if ctrinId != "" {
		where = tool.MapWhere("id_ctrin", "=", ctrinId)
	}
	account := c.DefaultQuery("account", "")
	if account != "" {
		where = tool.MapWhere("account", "=", account)
	}
	name := c.DefaultQuery("name", "")
	if name != "" {
		where = tool.MapWhere("name", "=", name)
	}
	result, err := mysql.User{}.GetList(where, pageInt, limitInt)
	if err != nil {
		tool.Error(c, 1, err.Error())
		return
	}
	tool.SuccessAdmin(c, 0, "请求成功", result["count"].(int), result["data"])
}

func (ctr UserController) Edit(c *gin.Context) {

	id := c.DefaultQuery("id", "0")

	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id不正确")
		return
	} else {
		role, _ := mysql.User{}.GetItemById(ids)

		c.HTML(200, "system/user/edit.html", gin.H{
			"item": role,
		})
	}
}

func (ctr UserController) EditAjax(c *gin.Context) {

	id := c.PostForm("id")
	name := c.PostForm("name")
	headImg := c.PostForm("head_img")
	phone := c.PostForm("phone")
	isVip := c.PostForm("is_vip")
	etime := c.PostForm("expiration_time")

	toBeCharge := etime
	timeLayout := "2006-01-02 15:04:05"
	loc, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc)
	sr := theTime.Unix()

    mItem := map[string]interface{}{
    	"name":name,
    	"head_url":headImg,
    	"phone":phone,
    	"is_vip":isVip,
    	"expiration_time":sr,
	}

	db := mysql.Db

	err := db.Table("k_user_info").Where("id_user = ?",id).Update(mItem).Error
    if err!= nil {
		tool.Error(c, 0, err.Error())
		return
	}

	tool.Success(c, 200, "修改成功", "")
}
func (ctr UserController) Del(c *gin.Context) {

	id := c.DefaultQuery("id", "0")
	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id参数错误")
		return
	} else {

		errs := mysql.User{}.Del(ids)
		if errs != nil {
			tool.Error(c, 0, errs.Error())
			return
		}
		tool.Success(c, 200, "删除成功", "")
	}

}
func (ctr UserController) OnlineDel(c *gin.Context) {

	id := c.DefaultQuery("id", "0")
	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id参数错误")
		return
	} else {

		errs := mysql.User{}.OnlineDel(ids)
		if errs != nil {
			tool.Error(c, 0, errs.Error())
			return
		}
		tool.Success(c, 200, "删除成功", "")
	}

}
func (ctr UserController) Status(c *gin.Context) {
	id := c.DefaultQuery("id", "0")
	status := c.DefaultQuery("status", "0")

	ids, err := strconv.Atoi(id)
	if ids < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	statuss, err := strconv.Atoi(status)
	if statuss < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	errs := mysql.User{}.SetStatus(ids, statuss)

	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}

	tool.Success(c, 200, "操作成功", "")

}


func (ctr UserController) Online (c *gin.Context){

	c.HTML(200, "system/useronline/index.html", "")
}

func (ctr UserController) GetOnList(c *gin.Context) {

	page := c.DefaultQuery("page", "1")
	limit := c.DefaultQuery("limit", "15")
	where := []map[string]interface{}{}
	pageInt, err := strconv.Atoi(page)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	limitInt, err := strconv.Atoi(limit)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	ctrinId := c.Query("ctrinId")
	if ctrinId != "" {
		where = tool.MapWhere("id_ctrin", "=", ctrinId)
	}
	account := c.DefaultQuery("account", "")
	if account != "" {
		where = tool.MapWhere("account", "=", account)
	}
	name := c.DefaultQuery("name", "")
	if name != "" {
		where = tool.MapWhere("name", "=", name)
	}
	result, err := mysql.User{}.GetOnList(where, pageInt, limitInt)
	if err != nil {
		tool.Error(c, 1, err.Error())
		return
	}
	tool.SuccessAdmin(c, 0, "请求成功", result["count"].(int), result["data"])
}